#include <REG51.H>
#include <Buzzer.h>
#include <Clock.h>
#include <Key.h>
#include <Mode.h>

sbit Buzzer = P3 ^ 0; // 蜂鸣器

// 一次完整周期整点报时 响铃 次数
static char Buzzer_num = 0;

/**
 * @brief  蜂鸣器初始化
 * @param  None
 * @retval None
 */
void Buzzer_init(void)
{
    Buzzer = 0;
}

/**
 * @brief  蜂鸣器复位
 * @param  None
 * @retval None
 */
void Buzzer_reset(void)
{
    Buzzer = 0;
    Buzzer_num = 0;
}

/**
 * @brief  整点报时控制控制（由时钟滴答控制）
 * @param  None
 * @retval None
 */
void Buzzer_Hour_report(void)
{
    static bit Clock_Tick_bak; // 备份时钟滴答

    switch (Buzzer_num)
    {
    case 11:
        if (Clock_Tick_bak != Clock_Tick)
        {
            Clock_Tick_bak = Clock_Tick;
            Buzzer_num++;
        }
        break;

    case 12:
        if (Clock_Tick_bak != Clock_Tick)
        {
            Clock_Tick_bak = Clock_Tick;
            Buzzer = 0;
            Buzzer_num = 0;
        }
        break;

    default:
        if (Clock_Tick_bak != Clock_Tick)
        {
            Clock_Tick_bak = Clock_Tick;
            Buzzer = ~Buzzer;
            Buzzer_num++;
        }
        break;
    }
}

/**
 * @brief  整点报时控制（即手动取消）
 * @param  None
 * @retval None
 */
void Buzzer_ctrl(void)
{
    while (System_Mode == Mode_hour_report)
    {
        if (Key_Scan() != Key_NO_Down)
        {
            System_Mode = Mode_Clock;
            Buzzer = 0;
            Buzzer_num = 0;
        }
    }
}
